package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.king.zxing.util.CodeUtils;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityQrCreateBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class QrCreateActivity extends BaseAc<ActivityQrCreateBinding> {

    private boolean isCreate = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_qr_create;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivQrCreateBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivQrCreateShare.setOnClickListener(this);
        mDataBinding.tvCreate.setOnClickListener(this);
        mDataBinding.ivUrlClear.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivQrCreateShare://分享
                if (!isCreate) {
                    ToastUtils.showShort(getString(R.string.no_create_qr));
                    return;
                }
                //保存到最近文件
                String filePath = FileUtil.generateFilePath(Content.Temp, ".png");
                ImageUtils.save(ImageUtils.view2Bitmap(mDataBinding.ivQrCreateImg), filePath, Bitmap.CompressFormat.PNG);
                IntentUtil.shareImage(mContext, "", filePath);
                break;
            case R.id.tvCreate://生成二维码
                String text = mDataBinding.etUrl.getText().toString();
                if (TextUtils.isEmpty(text)) {
                    ToastUtils.showShort(R.string.please_input_qr_content);
                    return;
                }
                showDialog(getString(R.string.create_ing));
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        createQr(text);
                    }
                }, 1000);
                break;
            case R.id.ivUrlClear://url清空
                mDataBinding.etUrl.setText("");
                break;
        }
    }

    @Override
    protected void initData() {

    }

    //生成二维码
    private void createQr(String content) {
        new Thread(() -> {
            //生成二维码相关放在子线程里面
            Bitmap bitmap = CodeUtils.createQRCode(content, 284);
            runOnUiThread(() -> {
                //显示二维码
                mDataBinding.ivQrCreateImg.setImageBitmap(bitmap);
                mDataBinding.llQrCreate.setVisibility(View.VISIBLE);
                dismissDialog();
                isCreate = true;
                ToastUtils.showShort(R.string.qr_create_suc);
            });
        }).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FileUtils.deleteAllInDir(PathUtils.getFilesPathExternalFirst() + Content.Temp);
    }
}